package cnm.radio;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Calendar;

import cnm.red5.CnmPlayItem;
import cnm.red5.CnmServerStream;
import cnm.dao.*;
import cnm.model.*;
import cnm.storage.StorageService;

import org.red5.server.WebScope;

public class CnmRadioCronJob {
	private WebScope webScope;
	private ProgramDAO programDAO;	
	private StorageService storageService;

	public void setWebScope(WebScope webScope) {
		this.webScope = webScope;
	}	
	
	public void setProgramDAO(ProgramDAO programDAO) {
		this.programDAO = programDAO;
	}
	
	public void setStorageService(StorageService storageService) {
		this.storageService = storageService;
	}
	
	public void init() {
		publish();
	}
	
	@SuppressWarnings("unchecked")
	public void publish() {			
		List<CnmPlayItem> list = updatePlayList();
		CnmServerStream serverStream = new CnmServerStream(list);
		serverStream.setName("live_radio");
		serverStream.setPublishedName("live_radio");
		serverStream.setScope(webScope);
		serverStream.start();
	}
	
	public List<CnmPlayItem> updatePlayList() {
		//TODO: now get the latest 14 programs to make the list
		Calendar now = Calendar.getInstance();
		String monthStr = (now.get(Calendar.MONTH) + 1) < 10 ?
				"0" + (now.get(Calendar.MONTH) + 1) :
				"" + (now.get(Calendar.MONTH) + 1);
		String dayStr = now.get(Calendar.DAY_OF_MONTH) < 10 ?
				"0" + now.get(Calendar.DAY_OF_MONTH) :
				"" + now.get(Calendar.DAY_OF_MONTH);
		String dateStr = now.get(Calendar.YEAR) + "-" + monthStr + "-" + dayStr;
		String timeStr = ":00:00";
		System.out.print("dateStr:"+dateStr);
		
		List<Program> list = new ArrayList<Program>();
		try {
			list = storageService.loadPlayList(dateStr);    		
    		if (list == null) {
        		Sorter sorter = Sorter.desc("programDate");
        		list = programDAO.findAll(0, 14, sorter);
        		storageService.savePlayList(list, dateStr);
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
		List<CnmPlayItem> itemList = new ArrayList<CnmPlayItem>();
		for (int i = 0; i < list.size(); i++) {
			try { 
				int hour = 21 - i;
				String hourStr = null;
				if (hour < 10) {
					hourStr = "0" + hour;
				} else {
					hourStr = "" + hour;
				}
				String startDateStr = dateStr + "," + hourStr + timeStr;
				Date startDate = df.parse(startDateStr);
				String name = list.get(i).getProgramUuid() + ".mp3";
				CnmPlayItem item = new CnmPlayItem();
				item.setName(name);
				item.setStartDate(startDate);
				itemList.add(item);
				System.out.println("updatePlayList...date:"+item.getStartDate()+" name:"+item.getName());
			} catch (ParseException e) {
				e.printStackTrace();
			}			
		}
		return itemList;
	}
	
}
